<template>
  <!-- 编辑弹出框 id,product_id,href,seq_recommended,seq_new_arrival,seq_top_sale -->
  <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
    <el-form ref="dataForm" :model="dataForm" label-width="100px">
      <el-form-item label="ID" >
        <el-input v-model="dataForm.id" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="商品id">
        <!-- <el-input v-model="dataForm.product_id"></el-input> -->
        <product-select :id.sync="dataForm.product_id"></product-select>
      </el-form-item>
      <el-form-item label="外链url(可选)">
        <el-input v-model="dataForm.href"></el-input>
      </el-form-item>
      <el-form-item label="推荐序号">
        <el-input v-model="dataForm.seq_recommended" type="number" ></el-input>
      </el-form-item>
      <el-form-item label="新品序号">
        <el-input v-model="dataForm.seq_new_arrival" type="number"></el-input>
      </el-form-item>
      <el-form-item label="销售序号">
        <el-input v-model="dataForm.seq_top_sale" type="number"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" size="medium" @click="dataFormSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import ProductSelect from  "./ProductSelect.vue"
  import * as HomeAPI from '@/api/home.js';
  const defaultForm = {
    id: 0,
    product_id: null,
    href: null,
    seq_recommended: 0,
    seq_new_arrival: 0,
    seq_top_sale: 0,
  };

  export default {
    name: 'homeProductEdit',
    components: {
      ProductSelect,
    },
    data() {
      return {
        dataForm: Object.assign({}, defaultForm), //表单
        dialogVisible: false, //编辑框
      };
    },
    computed: {
      //弹框标题
      dialogTitle() {
        return !this.dataForm.id ? '新增' : '编辑';
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0;
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          if (id) {
            //如果是编辑
            HomeAPI.getHomeProductDetailById(id)
              .then(result => {
                this.dataForm = result.data; //看接口文档，知道data是对应的用户对象
              })
              .catch(error => {});
          } else {
            //如果是新增
            this.dataForm = Object.assign({}, defaultForm);
          }
        });
      },

      //提交确认
      dataFormSubmit() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            HomeAPI.updateHomeProduct(this.dataForm)
              .then(() => {
                this.$message.success(`保存成功`);
                this.dialogVisible = false;
                this.$emit('refreshDataList'); //重新刷新列表数据
              })
              .catch(error => {});
          }
        });
      },
    }
  };
</script>

<style scoped>
</style>
